// pages/coupon/use.js
const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cityIndex:0,
        modelIndex:0
     
      

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;   
        var open=wx.getStorageSync('open');
        console.log(open);
     //   open.phone=null
        if(open.phone===null){
            wx.navigateTo({
              url: '../mine/bindPhone',
            })
        }
    
   
        that.setData({
            platform:app.globalData.platform,       
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl,
            uploadUrl:app.globalData.uploadUrl,
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getCity();
        this.getModel();
    },
    getCity:function(){
        let that=this;
        var open=wx.getStorageSync('open');
        wx.request({        
            url: app.globalData.platUrl+'coupon/getCity',
            data:{from:1,user_id:open.user_id},
            success:res=>{
                console.log(res);
                if(res.statusCode==200){
                    let data=res.data;
                    //var city = Object.values(data.city); 
                    var city=[];
                    var citys=data.city;
                    citys.forEach(function(item,index){
                        city[item.id]=item.city;
                    });
                    that.setData({
                        citys:citys,
                        city:city,
                     //   coupon_no:coupon_no,
                        use:true
                    })
                }else{
                   wx.showLoading({
                     title: '系统错误',
                   }) 
                }
            }
            })
    },
    getModel(){
        let that=this;
        var open=wx.getStorageSync('open');
        wx.request({        
            url: app.globalData.platUrl+'coupon/getModel',
            data:{from:1,user_id:open.user_id},
            success:res=>{
                console.log(res);
                if(res.statusCode==200){
                    let data=res.data;
                    var model=[];
                    var models=data.model;
                    models.forEach(function(item,index){
                        model[item.id]=item.name;
                    });
                    that.setData({
                        model:model,
                        models:models
                    })
                }else{
                   wx.showLoading({
                     title: '系统错误',
                   }) 
                }
            }
            })
    },
    cityChange:function(e){
        //console.log(e)
        let that=this;
        var index=e.detail.value;
        var cityIndex=this.data.cityIndex;
        var citys=this.data.citys;
        var open=wx.getStorageSync('open');
        var city_id=citys[index].id;  
        this.setData({
            cityIndex:index,
            city_id:city_id      
        })
    },
    modelChange:function(e){
        //console.log(e)
        let that=this;
        var index=e.detail.value;
        var modelIndex=this.data.modelIndex;
        var models=this.data.models;       
        var model_id=models[index].id;  
        this.setData({
            modelIndex:index,
            model_id:model_id      
        })
    },
      inputUserPhone:function(e){
        console.log(e);
        var value=e.detail.value;
        if(value){
            console.log(value);
            this.setData({
                userphone:value
            })
        }else{
            this.setData({
                userphone:undefined
            }) 
        }
       
    },
    inputUserName:function(e){
        console.log(e);
        var value=e.detail.value;
        if(value){
            console.log(value);
            this.setData({
                username:value
            })
         }else{
            this.setData({
                username:undefined
            }) 
        }
    
    },
  
    showModals:function(msg){
        wx.showModal({
            title: '系统提示',
            content:msg,
            success:function(res){
                return false;
            }
        })
    },
    toSubmit:function(e){
        let that = this;
        var open = wx.getStorageSync('open');
        var store_id =wx.getStorageSync('store_id');
        let data=this.data;
        console.log(data);
        if(data.username==undefined){            
            this.showModals('请输入客户姓名');
        }else if(data.userphone==undefined){
            this.showModals('请输入客户电话');
        }else if(!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(data.userphone))){
          this.showModals('请输入正确的电话号码');
        }else if(data.cityIndex==0){
            this.showModals('请选择城市');        
        }else if(data.modelIndex==0){
            this.showModals('请选择意向车型');
        }else{
                
            var city_id = data.city_id;         
            var model_id = data.model_id
            wx.request({
                url: app.globalData.platUrl+'coupon/setConsult',
                data:{
                    from:1,
                    username:data.username,
                    userphone:data.userphone,
                    user_id:open.user_id,
                    store_id:store_id,
                    city_id:city_id,
                    model_id:model_id,
                  
                   
                },
                success:res=>{
                    console.log(res);
                    var data=res.data;
                    if(data.status==200){
                        wx.showModal({
                            title: '系统提示',
                            content:data.message,
                            success:function(res){
                                //that.useClose();
                                //that.getList();
                               wx.navigateBack();
                             
                                that.setData({
                                    use:false
                                })
                            }
                        })
                    }else{
                        wx.showModal({
                            title: '系统提示',
                            content:data.message,
                            success:function(res){
                                return false;
                            }
                        })
                    }
                }
            })
        }
    },

    goBack(){
        wx.navigateBack();
      },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})